
#include <cmath>
#include "metalwidgets.h"

using namespace std;
using namespace net::lliurex::lgi;

cairo_surface_t * srf_lcd;
cairo_surface_t * srf_led;
cairo_surface_t * srf_led_off;
cairo_surface_t * srf_manifold;
cairo_surface_t * srf_button;
cairo_surface_t * srf_button_pressed;
cairo_surface_t * srf_button_off;
cairo_surface_t * srf_button_off_pressed;
cairo_surface_t * srf_panic_button;
cairo_surface_t * srf_panic_button_over;
cairo_surface_t * srf_panic_button_pressed;
cairo_surface_t * srf_slider;
cairo_surface_t * srf_slider_button;
cairo_surface_t * srf_screen;

void MetalWidgets::LoadResources()
{
	srf_lcd = cairo_image_surface_create_from_png("data/lcd.png");
	srf_led = cairo_image_surface_create_from_png("data/led.png");
	srf_led_off = cairo_image_surface_create_from_png("data/led_off.png");
	srf_manifold = cairo_image_surface_create_from_png("data/manifold.png");
	srf_button = cairo_image_surface_create_from_png("data/button.png");
	srf_button_pressed = cairo_image_surface_create_from_png("data/button_pressed.png");
	srf_button_off = cairo_image_surface_create_from_png("data/button_off.png");
	srf_button_off_pressed = cairo_image_surface_create_from_png("data/button_off_pressed.png");
	srf_panic_button = cairo_image_surface_create_from_png("data/panic_button.png");
	srf_panic_button_over = cairo_image_surface_create_from_png("data/panic_button_over.png");
	srf_panic_button_pressed = cairo_image_surface_create_from_png("data/panic_button_pressed.png");
	srf_slider = cairo_image_surface_create_from_png("data/slider.png");
	srf_slider_button = cairo_image_surface_create_from_png("data/slider_button.png");
	srf_screen = cairo_image_surface_create_from_png("data/screen.png");
}


LCD::LCD(int x,int y)
{
	this->x=x;
	this->y=y;
	this->name=string("LCD");

	this->mouse_over=false;
	this->mouse_press=false;
	
	this->width=cairo_image_surface_get_width(srf_lcd);
	this->height=cairo_image_surface_get_height(srf_lcd);

}
		
void LCD::Draw(cairo_t * cairo)
{
	cairo_set_source_surface(cairo,srf_lcd,x,y);
	cairo_paint (cairo);
	
	cairo_select_font_face (cairo, "Electronic Highway Sign", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
	cairo_set_font_size (cairo, 16.0);
	
	cairo_set_source_rgb (cairo, 0.3, 0.3, 0.3);
	cairo_move_to (cairo, x+16,y+28);
	cairo_show_text (cairo, text1.c_str());
	
	cairo_move_to (cairo, x+16,y+48);
	cairo_show_text (cairo, text2.c_str());
	
}



		
LED::LED(int x,int y,string label)
{
	this->x=x;
	this->y=y;
	this->name=string("LED");

	this->mouse_over=false;
	this->mouse_press=false;
	
	this->status=false;
	
	this->width=cairo_image_surface_get_width(srf_led);
	this->height=cairo_image_surface_get_height(srf_led);
	this->label=label;
}
		
void LED::Draw(cairo_t * cairo)
{
	cairo_text_extents_t te;
	cairo_font_extents_t fe;
	
	if(status)
	{
		cairo_set_source_surface(cairo,srf_led,x,y);
		cairo_paint (cairo);
	}
	else
	{
		cairo_set_source_surface(cairo,srf_led_off,x,y);
		cairo_paint (cairo);
	}
	
	cairo_select_font_face (cairo, "Embossing Tape 1 (BRK)", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
	cairo_set_font_size (cairo, 11.0);
	cairo_text_extents (cairo, label.c_str(), &te);
	cairo_font_extents (cairo, &fe);
	cairo_set_source_rgb(cairo,0.1,0.1,0.1);
	cairo_move_to (cairo, x+(width/2.0)-(te.width/2.0),y+height-4);
	cairo_show_text (cairo, label.c_str());
	
}
	



	
Manifold::Manifold(int x,int y)
{
	this->x=x;
	this->y=y;
	this->name=string("Manifold");

	this->mouse_over=false;
	this->mouse_press=false;
	
	this->width=cairo_image_surface_get_width(srf_manifold);
	this->height=cairo_image_surface_get_height(srf_manifold);
	
	this->value=0.4f;
}
		
void Manifold::Draw(cairo_t * cairo)
{
	
	cairo_set_source_surface(cairo,srf_manifold,x,y);
	cairo_paint (cairo);
	
	float fx,fy;
	if(value<0.0f)value=0.0f;
	if(value>1.0f)value=1.0f;
	
	double angle = (1.0f-value) * ( 3.14159 + (3.14159/2.0));
	fx = 24.0 * cos(angle);
	fy = 24.0 * sin(angle);
	
	cairo_set_source_rgb(cairo,0.9,0.1,0.1);
	cairo_move_to(cairo,x+32,y+32);
	cairo_line_to(cairo,x+32+fx,y+32-fy);
	cairo_stroke(cairo);
}


Panic::Panic(int x,int y)
{
	this->x=x;
	this->y=y;
	this->name=string("Panic");

	this->mouse_over=false;
	this->mouse_press=false;
	
	this->width=cairo_image_surface_get_width(srf_panic_button);
	this->height=cairo_image_surface_get_height(srf_panic_button);
}

void Panic::Draw(cairo_t * cairo)
{
	if(mouse_press)
	{
		cairo_set_source_surface(cairo,srf_panic_button_pressed,x,y);
		cairo_paint(cairo);
	}	
	else
	{
		if(mouse_over)
		{
			cairo_set_source_surface(cairo,srf_panic_button_over,x,y);
			cairo_paint(cairo);
		}
		else
		{
			cairo_set_source_surface(cairo,srf_panic_button,x,y);
			cairo_paint(cairo);
		}
	}
}


Button::Button(int x,int y,string text)
{
	this->x=x;
	this->y=y;
	this->name=string("Switcher");

	this->mouse_over=false;
	this->mouse_press=false;
	
	this->width=cairo_image_surface_get_width(srf_button);
	this->height=cairo_image_surface_get_height(srf_button);
	
	this->status=false;
	this->text=text;
}
	
	
void Button::Draw(cairo_t * cairo)
{
	cairo_text_extents_t te;
	cairo_font_extents_t fe;
	
	

	if(status)
	{
		if(mouse_press)
		{
			cairo_set_source_surface(cairo,srf_button_pressed,x,y);
		}
		else
		{
			cairo_set_source_surface(cairo,srf_button,x,y);
		}
		
		cairo_paint(cairo);
	}
	else
	{
		if(mouse_press)
		{
			cairo_set_source_surface(cairo,srf_button_off_pressed,x,y);
		}
		else
		{
			cairo_set_source_surface(cairo,srf_button_off,x,y);
		}
		
		cairo_paint(cairo);
	}
	
	cairo_select_font_face (cairo, "Ubuntu", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
	cairo_set_font_size (cairo, 14.0);
	cairo_text_extents (cairo, text.c_str(), &te);
	cairo_font_extents (cairo, &fe);
	
	cairo_set_source_rgb (cairo, 0.3, 0.3, 0.3);
	if(mouse_press)
		cairo_move_to (cairo, 2+x+(width/2.0)-(te.width/2.0),2+y+(height/2.0)+(fe.height/2.0));
	else
		cairo_move_to (cairo, x+(width/2.0)-(te.width/2.0),y+(height/2.0)+(fe.height/2.0));
		
	cairo_show_text (cairo, text.c_str());
}


		
Slider::Slider(int x,int y,string text)
{
	this->x=x;
	this->y=y;
	this->name=string("Slider");

	this->mouse_over=false;
	this->mouse_press=false;
	
	this->width=cairo_image_surface_get_width(srf_slider);
	this->height=cairo_image_surface_get_height(srf_slider);
	
	
	this->text=text;
	this->value=0.0f;
	
	this->grab=false;
	
}
		
		
void Slider::Draw(cairo_t * cairo)
{
	if(value>1.0f)value=1.0f;
	if(value<0.0f)value=0.0f;
	
	cairo_set_source_surface(cairo,srf_slider,x,y);
	cairo_paint(cairo);
	
	float ys = 14.0f + value*100.0f;
	float sw,sh;
	
	sw = cairo_image_surface_get_width(srf_slider_button);
	sh = cairo_image_surface_get_height(srf_slider_button);
	
	cairo_set_source_surface(cairo,srf_slider_button,x+(width/2.0)-(sw/2.0),y+height-ys-(sh/2.0));
	cairo_paint(cairo);
}
		
void Slider::OnMouseDown(MouseDownEvent * event)
{
	float sw,sh;
	float sx,sy;
	
	sw = cairo_image_surface_get_width(srf_slider_button);
	sh = cairo_image_surface_get_height(srf_slider_button);
	
	float ys = 14.0f + value*100.0f;
	
	sx=x+(width/2.0)-(sw/2.0);
	sy=y+height-ys-(sh/2.0);
	
	if(event->x > sx && event->x < (sx+sw))
		if(event->y > sy && event->y < (sy+sh))
		{
			grab=true;
			grab_y=event->y;
			grab_value=value;
			return;
		}
		
	grab=false;
}

void Slider::OnMouseUp(MouseUpEvent * event)
{
	grab=false;
}

void Slider::OnMouseMove(MouseMoveEvent * event)
{
	if(grab)
	{
		float dy = grab_y - event->y;
		value = grab_value + (dy/100.0f);
		//cout<<"diff y "<<dy/100.0f<<endl;
	}
}
		
	



		
TvScreen::TvScreen(int x,int y,string text)
{
	this->x=x;
	this->y=y;
	this->name=string("Screen");

	this->mouse_over=false;
	this->mouse_press=false;
	
	this->width=cairo_image_surface_get_width(srf_screen);
	this->height=cairo_image_surface_get_height(srf_screen);
	
	
	this->text=text;
	this->vx=0.0f;
	this->vy=0.0f;
}
		
void TvScreen::Draw(cairo_t * cairo)
{
	if(vx>1.0f)vx=1.0f;
	if(vy>1.0f)vy=1.0f;
	if(vx<-1.0f)vx=-1.0f;
	if(vy<-1.0f)vy=-1.0f;
	
	cairo_set_source_surface(cairo,srf_screen,x,y);
	cairo_paint(cairo);
	
	float px = x+14.0f+((vx+1.0f)*50.0f);
	float py = y+14.0f+((vy+1.0f)*50.0f);
	
	cairo_set_source_rgb(cairo,0.8,0.9,0.8);
	cairo_move_to(cairo,px-2.0f,py);
	cairo_line_to(cairo,px+2.0f,py);
	cairo_stroke(cairo);
	
	cairo_move_to(cairo,px,py-2.0f);
	cairo_line_to(cairo,px,py+2.0f);
	cairo_stroke(cairo);
	
	cairo_select_font_face (cairo, "Electronic Highway Sign", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
	cairo_set_font_size (cairo, 10.0);
	
	cairo_move_to (cairo, x+16.0,y+height-16.0f);
	cairo_show_text (cairo, text.c_str());
}


Label::Label(int x,int y,string text)
{
	this->x=x;
	this->y=y;
	this->name=string("Label");
	this->text=text;
	
	/* we can not set real dimensions before first Draw call */
	this->width=5;
	this->height=5;

}

void Label::Draw(cairo_t * cairo)
{
	cairo_text_extents_t te;
	cairo_font_extents_t fe;
	
	cairo_select_font_face (cairo, "Embossing Tape 1 (BRK)", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
	cairo_set_font_size (cairo, 16.0);
	cairo_text_extents (cairo, text.c_str(), &te);
	cairo_font_extents (cairo, &fe);
	
	width=te.width;
	height=(fe.height);
	width+=20.0f; // 10+10 margins 
	height+=(height*0.8f);
	
	cairo_set_source_rgb(cairo, 0.72, 0.72, 0.72);
	
	cairo_rectangle(cairo,x,y,width,height);
	cairo_fill(cairo);
	
	cairo_set_source_rgb (cairo, 0.05, 0.05, 0.05);
	cairo_move_to (cairo, x+((width-te.width)/2.0)+1,y+(height)-((height-fe.height)/2.0)-fe.descent+1);
	cairo_show_text (cairo, text.c_str());
	
	
}
